package com.tansun.easycare.rule.codeblock.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.codeblock.model.CodeblockVO;
import com.tansun.easycare.rule.codeblock.service.RuleCodeblockService;
import com.tansun.easycare.rule.common.DataCache;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.common.web.model.ProjectInfo;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.rule.utils.LoggerManager;

/**
 * 自定义函数（规则函数）
 *
 * @author WYH 2017年9月11日
 */
@RestController
@RequestMapping(value = "/ruleProj/func")
public class RuleFunctionController extends RuleBaseController {
    @Autowired
    private RuleManageService ruleManageService;

    @Autowired
    private RuleCodeblockService ruleCodeblockService;

    @RequestMapping("index")
    public @ResponseBody ResponseTable<RuleManageVO> index(@RequestBody RuleManageVO param) throws Exception {
    	PageInfo<RuleManageVO> pageInfo = this.ruleManageService.findRuleManageVO(param.getPage(), param);
        ResponseTable<RuleManageVO>  resVo  = new  ResponseTable<RuleManageVO>(pageInfo);
        return resVo;   	
    }

    /**
     * 跳转至编辑页面(修改或查看)
     */
    @RequestMapping("toEdit")
    public ResponseVO toEdit(@RequestBody RuleManage manage) throws Exception {
    	ResponseVO res = new ResponseVO();
    	 try {
    		 CodeblockVO vo = this.ruleCodeblockService.findCodeblocks(manage.getId(),manage.getProjectCode());
    		 vo.setProjectCode(manage.getProjectCode());
    		 res.setData(vo);
    	 }catch(Exception e) {
    		 LoggerManager.error(e.getMessage());
    		 res.setStatus(ResponseVO.ERROR);;
    		 res.setDescription("信息获取失败");
    	 }
    	return res;
    }

    /**
     * 保存规则函数信息
     */
    @RequestMapping("save")
    @ResponseBody
    public ResponseVO save(@RequestBody CodeblockVO model) throws Exception {
    	ResponseVO ret=new ResponseVO();
    	try {
            Integer num = this.ruleManageService.countListFindName(model.getRuleManage(), "19");
            if (num != null && num>0) {
                ret.setStatus(ResponseVO.ERROR);
                ret.setDescription("变量名称重复，请使用其他有效名称");
                return ret;
            }
    		ret=ruleCodeblockService.saveFunc(model);
    		if(ret.getStatus()==200) {
    			ret.setDescription("保存成功");
    	    	DataCache.funcParamChange(model.getProjectCode());
    		}
        } catch (Exception e) {
            LoggerManager.error(e.getMessage(), e, this.getClass());
            ret.setDescription(e.getMessage());;
            ret.setStatus(ResponseVO.ERROR);
        }
    	return ret;

    }

    /**
     * 编译校验规则函数信息
     */
    @RequestMapping("validate")
    @ResponseBody
    public ResponseVO validate(@RequestBody CodeblockVO model) throws Exception {
    	ResponseVO ret=new ResponseVO();
    	try {
    		ret=ruleCodeblockService.validateDrl(model,false);
    		if(ret.getStatus()==200) {
    			ret.setDescription("编译成功");
    		}
        } catch (Exception e) {
            LoggerManager.error(e.getMessage(), e, this.getClass());
            ret.setDescription(e.getMessage());;
            ret.setStatus(ResponseVO.ERROR);
        }
    	return ret;
    }

    /**
     * 删除规则函数
     */
    @RequestMapping("statusChange")
    public   ResponseVO statusChange(@RequestBody ProjectInfo projectInfo) throws Exception {
    	ResponseVO ret = new ResponseVO();
        try {
        	String ids[] = projectInfo.getManageId().split(",");
        	for(String id:ids){
        		CodeblockVO vo = ruleCodeblockService.findCodeblocks(id, projectInfo.getProjectCode());
        		ruleManageService.delete(vo.getRuleManage());
        		ruleCodeblockService.delete(vo.getRuleCodeblock());
        	}
        	DataCache.funcParamChange(projectInfo.getProjectCode());
        } catch (Exception e) {
            LoggerManager.error(e.getMessage(), e, this.getClass());
            ret.setStatus(ResponseVO.ERROR);
            if (e instanceof ApplicationContextException) {
                ret.setDescription(e.getMessage());
            } else {
                ret.setDescription("状态修改失败，请查看日志");
            }
        }
        return ret;
    }

    /**
     * 规则函数复制
     *
     * @param manageId
     * @return
     */
    /* @RequestMapping("copy")
     @ResponseBody
     public JsonMessage copyRule(String manageId,String rankRelationId) {
         JsonMessage resMessage = new JsonMessage();
    
         resMessage.setResult(true);
         RuleCodeblock ruleCodeblock = new RuleCodeblock();
         ruleCodeblock.setManageId(manageId);
         RuleCodeblock copyRuleCodeblock = new RuleCodeblock();
         try {
             List<RuleCodeblock> list = ruleCodeblockService.findList(ruleCodeblock);
             RuleCodeblock baseRuleCodeblock = list.get(0);
             BeanUtils.copyProperties(copyRuleCodeblock, baseRuleCodeblock);
             RuleManage baseRuleManage = ruleManageService.getById(manageId);
             RuleManage copyRuleManage = new RuleManage();
             BeanUtil.copyProperties(copyRuleManage, baseRuleManage);
    
             copyRuleCodeblock.setId(null);
             copyRuleCodeblock.setFunctionName(baseRuleCodeblock.getFunctionName() + "-copy");
             copyRuleCodeblock.setModelName(baseRuleCodeblock.getModelName() + "-copy");
             ruleCodeblockService.save(copyRuleCodeblock);

    
         } catch (Exception e) {
            resMessage.setResult(false);
             resMessage.setMessage(e.getMessage());
         }
         return resMessage;
     }*/

    @Override
    public void setRuleBizService() {
        super.setRuleBaseService(ruleCodeblockService);
    }


}
